﻿using AutoMapper;
using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MediaAdmin.Application.Infrastructure;
using System.ComponentModel;

namespace MediaAdmin.Controllers
{
    public partial class JobController
    {
        public static class Mappings
        {
            public static void GetMap()
            {
                Mapper.CreateMap<IJob, JobController.JobSummary>().ForMember(dst => dst.State, opt => opt.MapFrom(src => Enum.GetName(typeof(JobState), src.State)));
                Mapper.CreateMap<IJob, JobController.JobSummaryShortly>().ForMember(dst => dst.State, opt => opt.MapFrom(src => Enum.GetName(typeof(JobState), src.State)));
                Mapper.CreateMap<IAsset, JobController.AssetSummary>();
                Mapper.CreateMap<ITask, JobController.TaskSummary>().ForMember(dst => dst.ErrorDetails, opt => opt.MapFrom(src => src.ErrorDetails.Select(x => string.Format("[{0}] {1}", x.Code, x.Message)).ToEnuString()));
            }
        }

        public class JobSummaryShortly
        {
            public DateTime Created { get; set; }

            [DisplayName("End time")]
            public DateTime? EndTime { get; set; }
            public string Id { get; set; }
            public DateTime LastModified { get; set; }
            public string Name { get; set; }
            public int Priority { get; set; }
            [DisplayName("Running")]
            public TimeSpan RunningDuration { get; set; }
            public DateTime? StartTime { get; set; }
            public string State { get; set; }
            public string TemplateId { get; set; }
        }

        public class JobSummary
        {
            public DateTime Created { get; set; }

            [DisplayName("End time")]
            public DateTime? EndTime { get; set; }
            public string Id { get; set; }
            public List<AssetSummary> InputMediaAssets { get; set; }
            public DateTime LastModified { get; set; }
            public string Name { get; set; }
            public List<AssetSummary> OutputMediaAssets { get; set; }
            public int Priority { get; set; }
            [DisplayName("Running")]
            public TimeSpan RunningDuration { get; set; }
            public DateTime? StartTime { get; set; }
            public string State { get; set; }
            public List<TaskSummary> Tasks { get; set; }
            public string TemplateId { get; set; }
        }

        public class AssetSummary
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        public class TaskSummary
        {
            public string Configuration { get; set; }
            public string ErrorDetails { get; set; }
            public string MediaProcessorId { get; set; }
            public string Name { get; set; }
            public string PerfMessage { get; set; }
            public int Priority { get; set; }
            public double Progress { get; set; }
            public TimeSpan RunningDuration { get; set; }
            public DateTime? StartTime { get; set; }
            //public JobState State { get; set; }
            public string TaskBody { get; set; }           
        }

        public class CreateJobModel
        {
            [DisplayName("Job name")]
            public string Name { set; get; }

            public string AssetId { set; get; }

            [DisplayName("Encoding task")]
            public bool PerformEncoding { set; get; }

            [DisplayName("To smooth task")]
            public bool PerformSmooth { set; get; }

            [DisplayName("To HLS task (require smooth or encoding task)")]
            public bool PerformHLS { set; get; }

            [DisplayName("decrypt (media services bug workaround)")]
            public bool DecryptHLS { set; get; }

            [DisplayName("Thumbnails")]
            public bool PerformThumbnailing { set; get; }

            public string EncodingPresetName { set; get; }

            public string SmoothPresetName { set; get; }

            public string HLSPresetName { set; get; }

            public string ThumbnailPresetName { set; get; }

            [DisplayName("Asset")]
            public List<AssetSummary> AssetList { set; get; }

            [DisplayName("Preset")]
            public List<string> Presets { set; get; }
        }

        public class DeleteModel
        {
            public string Name { set; get; }
            public string Id { set; get; }
        }
    }
}